package budget.control


class TransactionReaderService {

    def process(File f) {
		
		f.eachLine { line, i ->
			System.out.println(i);
			def cols = line.split(",");
			//System.out.println(cols);
			def account = cols[0];
			System.out.println(account);
			Account a = Account.findOrSaveWhere([identifier: account, type: AccountType.CREDIT, name: account])
			def date = cols[3]
			def amount = cols[11] 
			if (amount == '""') {
				amount = cols[12]
				amount = - amount.toDouble()
			} else {
				amount = amount.toDouble()
			}
			date = Date.parse("yyyy/MM/dd", date)
			System.out.println(date);
			System.out.println(amount);
			System.out.println(cols[5]);//payee
			Expense e = new Expense(account: a, transactionDate: date, amount: amount)
			e.save()
			System.out.println(e);
			//a.transactions.add(e).save()
		}

    }
}
